﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DeviceManager
{
    class MyConfigueManager
    {
        public static string PATH = "conf.dat";
        List<System.Windows.Forms.TextBox> m_TextBoxs = new List<System.Windows.Forms.TextBox>();
        bool m_loading = false;

        public void AddControl(System.Windows.Forms.TextBox obj)
        {
            m_TextBoxs.Add(obj);
            obj.TextChanged += new EventHandler(obj_TextChanged);
        }

        void obj_TextChanged(object sender, EventArgs e)
        {
            Save();
        }
        
        public void Load()
        {
            if (!File.Exists(PATH)) return;
            m_loading = true;
            StreamReader sr = new StreamReader(PATH);            
            while (true)
            {
                String line = sr.ReadLine();
                if(line == null)break;
                String[] args = line.Split(new char[]{'\t'});
                for (int i = 0; i < m_TextBoxs.Count; i++)
                {
                    if (m_TextBoxs[i].Name == args[0])
                    {
                        m_TextBoxs[i].Text = args[1];
                    }
                }
            }
            sr.Close();
            m_loading = false;
        }

        public void Save()
        {
            if (m_loading) return;
            StreamWriter sw = new StreamWriter(PATH);
            for(int i = 0; i<m_TextBoxs.Count; i++){
                sw.WriteLine(m_TextBoxs[i].Name + "\t" + m_TextBoxs[i].Text);
            }
            sw.Close();
        }

    }
}
